package com.interesting.client.UI.home;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.interesting.MainActivity;
import com.interesting.R;
import com.interesting.base.core.BaseFragment;
import com.interesting.base.utils.Constant;
import com.interesting.client.UI.commonActivity.UIUtil;
import com.interesting.client.UI.setting.LoginActivity;
import com.interesting.client.UI.setting.MoreActivity;
import com.interesting.client.UI.user.UserActivity;
import com.interesting.client.application.AppHolder;

/** slidingMenu的menu界面 */
public class MenuFragment extends BaseFragment implements OnClickListener, OnCheckedChangeListener {
	private ImageView user_icon;

	private TextView user_name;
	private RadioGroup radiogroup;
	private RadioButton hot, recent, picture, mycollection, mycomment, mymessage, examine, set;
	private ActionBar bar;
	private RelativeLayout menu_fragment_user_layout;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.menu_fragment, container, false);
		menu_fragment_user_layout= (RelativeLayout)v.findViewById(R.id.menu_fragment_user_layout);
		user_icon = (ImageView) v.findViewById(R.id.menu_fragment_user_icon);
		user_name = (TextView) v.findViewById(R.id.menu_fragment_user_name);
		radiogroup = (RadioGroup) v.findViewById(R.id.menu_fragment_radiogroup);
		hot = (RadioButton) v.findViewById(R.id.menu_fragment_tohot);
		recent = (RadioButton) v.findViewById(R.id.menu_fragment_tonew);
		picture = (RadioButton) v.findViewById(R.id.menu_fragment_tophoto);
		mycollection = (RadioButton) v.findViewById(R.id.menu_fragment_my_collection);
		mycomment = (RadioButton) v.findViewById(R.id.menu_fragment_my_comment);
		mymessage = (RadioButton) v.findViewById(R.id.menu_fragment_my_message);
		examine = (RadioButton) v.findViewById(R.id.menu_fragment_my_examine);
		set = (RadioButton) v.findViewById(R.id.menu_fragment_set);
		bar = getSherlockActivity().getSupportActionBar();
		return v;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);


		String userName = AppHolder.getInsatnce().user.getUserName();
		String userIcon = AppHolder.getInsatnce().user.getUserIcon();
		if (userName != null && !userName.equals("")) {
			user_name.setText(userName);
		}
		if (userIcon != null && !userIcon.equals("")) {
			AppHolder.getInsatnce().imageLoader.DisplayImage(
					Constant.IMGPATH + "/" + AppHolder.getInsatnce().user.getUserIcon(), user_icon,
					R.drawable.ic_user_default);
		}
		hot.setChecked(true);
		hot.setOnClickListener(this);
		recent.setOnClickListener(this);
		picture.setOnClickListener(this);
		mycollection.setOnClickListener(this);
		mycomment.setOnClickListener(this);
		mymessage.setOnClickListener(this);
		examine.setOnClickListener(this);
		set.setOnClickListener(this);
		menu_fragment_user_layout.setOnClickListener(this);
		radiogroup.setOnCheckedChangeListener(this);

	}

	@Override
	public void onClick(View v) {

		Intent intent = null;
		switch (v.getId()) {
		case R.id.menu_fragment_user_layout: // 用户登录条
			if (UIUtil.isLoggedIn()) {
				intent = new Intent(getSherlockActivity(), UserActivity.class);
			} else {
				intent = new Intent(getSherlockActivity(), LoginActivity.class);
			}
			break;
		case R.id.menu_fragment_user_name: // 注销 登录
			
			break;
		case R.id.menu_fragment_tohot:// 热门
			((MainActivity) getSherlockActivity()).fManager.changFragment("hot");
			break;
		case R.id.menu_fragment_tonew:// 最近
			((MainActivity) getSherlockActivity()).fManager.changFragment("recent");
			break;
		case R.id.menu_fragment_tophoto:// 看图
			((MainActivity) getSherlockActivity()).fManager.changFragment("picture");
			break;
		// case R.id.menu_fragment_tovideo:// 视频
		// contentType = "video";
		// intent = new Intent(getSherlockActivity(),
		// VideoAndMusicActivity.class);
		// break;
		case R.id.menu_fragment_my_collection:// 我收藏的
			if (UIUtil.isLoggedIn()) {
				((MainActivity) getSherlockActivity()).fManager.changFragment("mycollect");
			} else {// 跳转到登录界面
				intent = new Intent(getSherlockActivity(), LoginActivity.class);
			}
			break;
		case R.id.menu_fragment_my_comment:// 我评论的
			if (UIUtil.isLoggedIn()) {
				((MainActivity) getSherlockActivity()).fManager.changFragment("mycomment");
			} else {// 跳转到登录界面
				intent = new Intent(getSherlockActivity(), LoginActivity.class);
			}
			break;
		case R.id.menu_fragment_my_message: // 我发表的
			if (UIUtil.isLoggedIn()) {
				((MainActivity) getSherlockActivity()).fManager.changFragment("mymessage");
			} else {// 跳转到登录界面
				intent = new Intent(getSherlockActivity(), LoginActivity.class);
			}
			break;
		case R.id.menu_fragment_my_examine: // 审核
			if (UIUtil.isLoggedIn()) {
				intent = new Intent(getSherlockActivity(), ExamineActivity.class);
			} else {
				intent = new Intent(getSherlockActivity(), LoginActivity.class);// 跳转到登录界面
			}
			break;
		case R.id.menu_fragment_set: // 更多
			intent = new Intent(getSherlockActivity(), MoreActivity.class);
			break;
		}
		if (intent != null) {
			radiogroup.clearCheck();
			((MainActivity) getSherlockActivity()).toActivity(intent);
		}
	}

	/** 用来变更actionbar的customView */
	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		RadioButton first, second;
		switch (checkedId) {
		case R.id.menu_fragment_tohot: // 热门
			bar.setCustomView(R.layout.two_radio_actionbar_custom_view);
			first = (RadioButton) bar.getCustomView().findViewById(R.id.two_radio_bar_first);
			second = (RadioButton) bar.getCustomView().findViewById(R.id.two_radio_bar_second);
			first.setText("干货");
			first.setTag("0");
			second.setText("嫩草");
			second.setTag("1");
			break;
		case R.id.menu_fragment_tonew:// 最近
			bar.setCustomView(R.layout.three_radio_actionbar_custom_view);
			break;
		case R.id.menu_fragment_tophoto:// 看图
			bar.setCustomView(R.layout.two_radio_actionbar_custom_view);
			first = (RadioButton) bar.getCustomView().findViewById(R.id.two_radio_bar_first);
			second = (RadioButton) bar.getCustomView().findViewById(R.id.two_radio_bar_second);
			first.setText("硬菜");
			first.setTag("2");
			second.setText("时令");
			second.setTag("3");
			break;

		case R.id.menu_fragment_my_collection:// 我收藏的
			if (UIUtil.isLoggedIn()) {
				((MainActivity) getSherlockActivity()).setTitle(getResources().getString(R.string.mycollect));
			}
			break;
		case R.id.menu_fragment_my_comment:// 我评论的
			if (UIUtil.isLoggedIn()) {

				((MainActivity) getSherlockActivity()).setTitle(getResources().getString(R.string.mycomment));

			}
			break;
		case R.id.menu_fragment_my_message: // 我发表的
			if (UIUtil.isLoggedIn()) {
				((MainActivity) getSherlockActivity()).setTitle(getResources().getString(R.string.mymessage));
			}
			break;
		}
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		Log.d("menu", requestCode + resultCode + "");
		switch (resultCode) {
		case 10: // 登录activity结束返回的结果码
			
			String userIcon = AppHolder.getInsatnce().user.getUserIcon();

			if (userIcon != null && !userIcon.equals("")) {
				user_name.setText(AppHolder.getInsatnce().user.getUserName());
				AppHolder.getInsatnce().imageLoader.DisplayImage(
						Constant.IMGPATH + "/" + AppHolder.getInsatnce().user.getUserIcon(), user_icon,
						R.drawable.ic_user_default);
			} else {
				user_icon.setImageResource(R.drawable.ic_sliding_user_default);
				user_name.setText("醉有趣");
			}
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	public void refresh() {
		user_name.setText(getResources().getString(R.string.user_name_dafault));
		user_icon.setImageDrawable(getResources().getDrawable(R.drawable.ic_sliding_user_default));
	}

	/** 上传用户头象 */
	
}
